package com.doctor.comm;

import com.doctor.bean.DoctorBean;
import com.doctor.bean.PatientFileBean;
import com.doctor.bean.UserBean;
import com.doctor.database.DbOperator;
import com.doctor.ui.LoginActivity;
import com.doctor.ui.R;
import com.doctor.ui.WebsiteActivity;
import com.doctor.ui.consulting.AnswerDoctorPhoneActivity;
import com.doctor.ui.homedoctor.AnswerPatientPhoneActivity;
import com.doctor.ui.homedoctor.LookPatientFileFrag;

import android.app.ActivityManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Telephony;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class AnswerPhoneReceiver extends BroadcastReceiver{

	/**
	 * 接听电话广播处理
	 */
	
	private static Context context;
	private static WindowManager wm = null;
	private static LinearLayout view = null;
	private static boolean isShowwing=false;
	private static String phone;
	//类型，1患者，2医生
	private static int typeInt=-1;
	private static PatientFileBean bean;
	private static DoctorBean doctorBean;
	
	@Override
	public void onReceive(final Context context, Intent intent) {
		// TODO Auto-generated method stub
		
		try {
//			phone= intent.getExtras().getString("incoming_number");
			phone = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
		} catch (NullPointerException e) {
			// TODO: handle exception
			e.printStackTrace();
			return;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		
//		TelephonyManager manager=(TelephonyManager) context.getApplicationContext().getSystemService(Service.TELEPHONY_SERVICE);
		TelephonyManager manager = (TelephonyManager) context.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
		int state=manager.getCallState();
		
		UserBean userBean=DbOperator.getInstance(context).selectUserInfo();
		if (userBean==null) {
			return;
		}
		String userName=userBean.getUsername();
		String passWord=userBean.getMm();
		if (userName==null || passWord==null) {
			return;
		}
		this.context=context;
		
		switch (state) {
		case TelephonyManager.CALL_STATE_IDLE:  //挂断
			if (wm!=null && isShowwing) {
				wm.removeView(view);
				isShowwing=false;
				typeInt=-1;
			}
			bean=null;
			doctorBean=null;
			break;

		case TelephonyManager.CALL_STATE_RINGING: //来电
			bean=null;
			doctorBean = null;
			bean= DbOperator.getInstance(context).getPatientFileByPhone(phone);
			doctorBean = null;
			if (bean==null) {
				doctorBean=DbOperator.getInstance(context).getDoctorByPhone(phone);
			}
			if (bean!=null) {
				typeInt=1;
			} else if (doctorBean!=null) {
				typeInt=2;
			} else if (bean==null && doctorBean==null) {
				typeInt=-1;
				return;
			}
			if (isShowwing) {
				return;
			}
			
			wm=(WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
			view=(LinearLayout) View.inflate(context.getApplicationContext(), R.layout.answer_phone_tip, null);
			
			WindowManager.LayoutParams params = new WindowManager.LayoutParams();  
			params.type = WindowManager.LayoutParams.TYPE_PHONE;
			params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
			params.width = WindowManager.LayoutParams.WRAP_CONTENT;  
			params.height = WindowManager.LayoutParams.WRAP_CONTENT;
			params.gravity= Gravity.TOP|Gravity.CENTER_HORIZONTAL;
			
			TextView type=(TextView) view.findViewById(R.id.type);
			TextView name=(TextView) view.findViewById(R.id.name);
			if (typeInt==1) {
				type.setText("患者");
				name.setText(bean.getName());
			} else if (typeInt==2) {
				type.setText("专家");
				name.setText(doctorBean.getRealname());
			}
			
			TextView phoneNum=(TextView) view.findViewById(R.id.phoneNum);
			phoneNum.setText(phone);
			
			ImageView cancelBtn=(ImageView) view.findViewById(R.id.cancelBtn);
			
			cancelBtn.setOnClickListener(new OnClickListener() {					
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					wm.removeView(view);
					isShowwing=false;
					typeInt=-1;
				}
			});
			wm.addView(view, params);
			isShowwing=true;
			break;
			
		case TelephonyManager.CALL_STATE_OFFHOOK: //通话中
			if (wm!=null && isShowwing) {
				wm.removeView(view);
				isShowwing=false;
				typeInt=-1;
			}
			if (bean!=null) {
				Intent patientIntent=new Intent(context,AnswerPatientPhoneActivity.class);
				patientIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
				patientIntent.putExtra(ConstConfig.BEAN, bean);
				context.startActivity(patientIntent);
			} else if (doctorBean!=null) {
				Intent doctorIntent=new Intent(context,AnswerDoctorPhoneActivity.class);
				doctorIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
				doctorIntent.putExtra(ConstConfig.BEAN, doctorBean);
				context.startActivity(doctorIntent);
			}
			bean=null;
			doctorBean=null;
			break;
		}
		phone=null;
	}
	
	
	
	

}
